package com.pickgliss.ui
{
	import com.pickgliss.instance.ComponentGroupManager;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.image.CustomBitmap;
	import com.pickgliss.utils.ClassUtils;
	import com.pickgliss.utils.ObjectUtils;
	import com.pickgliss.utils.StringUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;

	/**
	 * 创建Component的工厂 
	 * @author Herman
	 * 
	 */	
	public final class ComponentFactory
	{
		private static var _instance:ComponentFactory;
		private static var COMPONENT_COUNTER:int = 1;
		private var _allComponents:Dictionary;
		public static function get Instance():ComponentFactory
		{
			if(_instance == null)
			{
				_instance = new ComponentFactory(new ComponentFactoryEnforcer());
			}
			return _instance;
		}
		/**
		 * 分隔参数信息字符串
		 * @param args 参数信息字符串
		 * @return 参数数组
		 * 
		 */		
		public static function parasArgs(args:String):Array
		{
			var resultArgs:Array = args.split(",");
			for(var i:int = 0;i<resultArgs.length;i++)
			{
				StringUtils.trim(resultArgs[i]);
			}
			return resultArgs;
		}

		public function ComponentFactory(enforcer:ComponentFactoryEnforcer)
		{
			_model = new ComponentModel();
			_allComponents = new Dictionary();
			ClassUtils.uiSourceDomain = ApplicationDomain.currentDomain;
		}

		private var _model:ComponentModel;
		/**
		 * 创建组件
		 * @param stylename Component的样式名称
		 * @return 
		 * 
		 */		
		public function creat(stylename:String,args:Array = null):*
		{
			var com:*;
			if(_model.getComonentStyle(stylename))
			{
				com = creatComponentByStylename(stylename);
			}else if(_model.getBitmapSet(stylename) || ClassUtils.classIsBitmapData(stylename))
			{
				com = creatBitmap(stylename);
			}else if(_model.getCustomObjectStyle(stylename))
			{
				com = creatCustomObject(stylename);
			}else
			{
				com = ClassUtils.CreatInstance(stylename,args);
			}
			return com;
		}
		/**
		 * 创建bitmap 支持通过bitmapData 或者bitmap 的xml标签创建
		 * @param classname 类名
		 * @return bitmap
		 * 
		 */		
		public function creatBitmap(classname:String):Bitmap
		{
			var bitmapSet:XML = _model.getBitmapSet(classname);
			var bitmapData:BitmapData;
			var bitmap:CustomBitmap;
			if(bitmapSet == null)
			{
				if(ClassUtils.uiSourceDomain.hasDefinition(classname))
				{
					bitmapData = ClassUtils.CreatInstance(classname,[0,0]);
					bitmap = new CustomBitmap(bitmapData);
					_model.addBitmapSet(classname,new XML("<bitmapData resourceLink='"+classname+"' width='"+bitmap.width+"' height='"+bitmap.height+"' />"));
				}
			}else
			{
				if(bitmapSet.name() == ComponentSetting.BITMAPDATA_TAG_NAME)
				{
					bitmapData = creatBitmapData(classname);
					bitmap = new CustomBitmap(bitmapData,"auto",String(bitmapSet.@smoothing)=="true");
				}else
				{
					bitmap = ClassUtils.CreatInstance(classname);
				}
				ObjectUtils.copyPorpertiesByXML(bitmap,bitmapSet);
			}
			return bitmap
		}
		/**
		 * 创建bitmapData 支持通过bitmapData 或者bitmap 的xml标签创建
		 * @param classname 类名
		 * @return bitmapData
		 * 
		 */		
		public function creatBitmapData(classname:String):BitmapData
		{
			var bitmapSet:XML = _model.getBitmapSet(classname);
			if(bitmapSet == null)return null;
			var bitmapData:BitmapData;
			if(bitmapSet.name() == ComponentSetting.BITMAPDATA_TAG_NAME)
			{
				bitmapData = ClassUtils.CreatInstance(classname,[int(bitmapSet.@width),int(bitmapSet.@height)]);
			}else
			{
				bitmapData = ClassUtils.CreatInstance(classname)["btimapData"];
			}
			return bitmapData;
		}
		public function createComponentGroupByStylename(stylename:String,manager:Boolean=false):*
		{//创建需要按组管理的component
			var b : Boolean = ComponentGroupManager.Instance.hasInstanceClass(stylename);
			var component : Component;
			if(b)
			{
				component = ComponentGroupManager.Instance.getInstanceByKey(stylename);
				component.dispose();
			}
			else
			{
				component = creatComponentByStylename(stylename);
				if(component.groups.length <= 0)
				{
					throw new Error("该实例没有分配组，请重载groups().");
				}
				ComponentGroupManager.Instance.addGroups(stylename,component);
			}
			return component;
		}
		/**
		 * 通过样式名称创建Component
		 * @param 样式名称
		 * @return Component
		 * 
		 */		
		public function creatComponentByStylename(stylename:String):*
		{
			var style:XML = getComponentStyle(stylename);
			var classname:String = style.@classname;
			var component:* = ClassUtils.CreatInstance(classname);
			component.id = componentID;
			_allComponents[component.id] = component;
			if(ClassUtils.classIsComponent(classname))
			{
				component.beginChanges();
				ObjectUtils.copyPorpertiesByXML(component,style);
				component.commitChanges();
			}else
			{
				ObjectUtils.copyPorpertiesByXML(component,style);
			}
			return component;
		}
		
		private function getComponentStyle(stylename:String):XML
		{
			var style:XML = _model.getComonentStyle(stylename);
			while(style.hasOwnProperty("@parentStyle"))
			{
				var parentStyle:XML = _model.getComonentStyle(String(style.@parentStyle));
				delete style.@parentStyle;
				ObjectUtils.combineXML(style,parentStyle);
			}
			return style;
		}
		
		private function getCustomStyle(stylename:String):XML
		{
			var style:XML = _model.getCustomObjectStyle(stylename);
			while(style.hasOwnProperty("@parentStyle"))
			{
				var parentStyle:XML = _model.getCustomObjectStyle(String(style.@parentStyle));
				delete style.@parentStyle;
				ObjectUtils.combineXML(style,parentStyle);
			}
			return style;
		}
		
		public function creatCustomObject(stylename:String, args:Array = null):*
		{
			var style:XML = getCustomStyle(stylename);
			var classname:String = style.@classname;
			var custom:* = ClassUtils.CreatInstance(classname, args);
			ObjectUtils.copyPorpertiesByXML(custom,style);
			return custom;
		}
		
		/**
		 * 通过Component::id获取Component
		 * @param componentid Component::id
		 * @return Component
		 * 
		 */		
		public function getComponentByID(componentid:int):*
		{
			return _allComponents[componentid];
		}
		/**
		 * 通过Component::id移除ComponentFactory中对Component的引用
		 * @param componentid Component::id
		 * 
		 */
		public function removeComponent(componentid:int):void
		{
			delete _allComponents[componentid];
		}
		
		/**
		 * 
		 * @param filterString : 代表滤镜的字符串  "null,f1|f2,f3,f4|f5"
		 * @return 包含滤镜的数组
		 * 
		 */		
		public function creatFrameFilters(filterString:String):Array
		{
			var filterStyles:Array = parasArgs(filterString);
			var resultFilters:Array = [];
			for(var i:int = 0;i<filterStyles.length;i++)
			{
				if(filterStyles[i] == "null")
				{
					resultFilters.push(null);
				}else
				{
					resultFilters.push(creatFilters(filterStyles[i]));
				}
			}
			return resultFilters;
		}
		
		public function creatFilters(filtersStyle:String):Array
		{
			var frameFilterStyle:Array = filtersStyle.split("|");
			var frameFilter:Array = [];
			for(var i:int = 0;i<frameFilterStyle.length;i++)
			{
				frameFilter.push(ComponentFactory.Instance.model.getSet(frameFilterStyle[i]));
			}
			return frameFilter;
		}
		
		public function get model():ComponentModel
		{
			return _model;
		}
		/**
		 * 
		 * @param url 配置Component的XML地址
		 * @param configRate 加载XML所占用的加载比率
		 * 
		 */		
		public function setup(config:XML):void
		{
			
			_model.addComponentSet(config);
		}
		
		public function get componentID():int
		{
			return COMPONENT_COUNTER++;
		}
	}
}

class ComponentFactoryEnforcer {
};